<!-- 模板 -->

<template>
  <FormSlot :label="`${GETTER.type}模板`" bind="planTemplateId" required>
    <div>
      <div v-if="one.planTemplateId">
        <span class="el-button--text" @click="toOpen">{{ templateName }}</span>
        <BtnText v-if="!isShow" title="清除" @click="toClear" />
      </div>

      <BtnPrimary v-else-if="!isShow" title="选择" @click="toOpen" />
    </div>

    <EasyModal v-model="easyVisible" :title="`选择${GETTER.type}模板`" @submit="toSubmit">
      <WebSearch path="template">
        <ParamInput label="模板名称" bind="templateName" />
      </WebSearch>

      <div class="Plan-MyTemplate-table FormChoose-MySingle">
        <WebTable path="template" @select="toSelect">
          <RowCheckbox />
          <RowTitle label="模板名称" bind="templateName" newOne :minWidth="120" />

          <RowTree v-if="one.planBizType === 1" v-bind="typeProps" />
          <RowDict v-else v-bind="typeProps" />

          <RowText label="说明" bind="templateExplain" :minWidth="120" />
          <RowText label="创建人" bind="createUserName" :width="120" />
          <RowTime label="创建时间" bind="createTime" />
        </WebTable>
      </div>
    </EasyModal>
  </FormSlot>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyTemplate',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [
    CORE.mixWeb(),
    CORE.mixWeb('template', 'template_'),
  ],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      model: {}, // 双向绑定对象
    }
  },

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 类型属性
     */
    typeProps() {
      const { planBizType: type } = this.one
      const { device_category_treeDict: categoryDict } = this.$root
      const label = type === 1 ? `${CORE.MAP.ASSET}类型` : `${CORE.MAP.REGION}类型`
      const bind = 'applyObjectId'
      const dict = type === 1 ? categoryDict : CORE.DICT.area_type
      return { label, bind, dict }
    },

    /**
     * 模板名称
     */
    templateName() {
      const { templateName = '', planBizType, applyObjectId } = this.one
      const { device_category_treeMap: categoryMap } = this.$root
      if (!templateName) return templateName || '-'

      const typeName = planBizType === 1
        ? (categoryMap[applyObjectId] || {}).assetTypeName
        : CORE.dict(CORE.DICT.area_type, applyObjectId)

      return `${templateName}${typeName ? ` (${typeName})` : ''}`
    },
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听计划类型
     */
    'one.planType'() {
      this.toClear()
      this.template_setList()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      const { planTemplateId: id, templateName, applyObjectId } = this.one

      this.easyVisible = true
      this.model = { id, templateName, applyObjectId }
      this.template_startList(1)
    },

    /**
     * 提交
     */
    toSubmit() {
      const { id, templateName, applyObjectId } = this.model
      CORE.merge(this.one, {
        planTemplateId: id || '',
        templateName: templateName || '',
        applyObjectId: applyObjectId || '',
      })
      this.easyVisible = false
    },

    /**
     * 清除
     */
    toClear() {
      CORE.merge(this.one, {
        planTemplateId: '',
        templateName: '',
        applyObjectId: '',
      })
    },

    /**
     * 点击复选
     */
    toSelect(idList) {
      const id = idList.pop()
      const chosen = this.template_list.find((item) => item.id === id) || {}

      this.template_param.idList = id ? [id] : []
      this.template_param.rowList = id ? [chosen] : []

      this.model = chosen
    },
  },
}
</script>

<style lang="scss">
@import './index.scss';
</style>
